package thread;

import java.sql.Array;
import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        Thread t1 = new Thread(){
            public void run(){
                for(int i=0;i<1000;i++){
                    synchronized(list){
                        list.add(i);
                    }

                }
                System.out.println("添加完毕");
            }
        };
        Thread t2 = new Thread(){
            public void run(){
                for(int i=0;i<1000;i++){
                    synchronized(list){
                        list.add(i);
                    }

                }
                System.out.println("添加完毕");
            }
        };
        t1.start();
        t2.start();

        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(list.size());
        Tese1 ts1 = new Tese1();

        Tese1 s = new Tese1();
        s.test();
    }
}


class Tese1 {
    List<Integer> list = new ArrayList<>();
    int size = 100;

     public void  test(){
         Thread t1 = new Thread(){
             @Override
             public void run() {
                 for(int i = 0;i < size;i ++){
                     list.add(i);
                     System.out.println("t1中的i："+i);
             }
             }
         };

         Thread t2 = new Thread(){
             @Override
             public void run() {
                 for(int i = 0;i < size;i ++){
                     list.add(i);
                 }
             }
         };

         t1.start();
         t2.start();
         //若不加时间，则直接执行  System.out.println(list.size());
         //导致list的大小为0；
         //侧面说明run是在主线程执行玩才开始；
         try {
             Thread.sleep(500);
         } catch (InterruptedException e) {
             e.printStackTrace();
         }
         System.out.println("test1执行结束！！！");
         System.out.println(list.size());
    }
}